import 'package:flutter/material.dart';

/**
 * 导航返回拦截
 */

class WillPopScopeDemo extends StatefulWidget {
  @override
  _WillPopScopeDemo createState() => new _WillPopScopeDemo();
}

class _WillPopScopeDemo extends State<WillPopScopeDemo> {
  //记录上次点击的时间
  DateTime _lastPressedAt;

  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
        child: Container(
          alignment: Alignment.center,
          child: Text("1秒内连续按两次返回键退出"),
        ),
        onWillPop: () async {
          if (_lastPressedAt == null ||
              DateTime.now().difference(_lastPressedAt) >
                  Duration(seconds: 1)) {
            _lastPressedAt = DateTime.now();
            return false;
          }
          return true;
        });
  }
}
